package com.example.userp.app.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RequestExecutor;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class PermissionActivity extends AppCompatActivity {

    private static String TAG = "PermissionActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        ButterKnife.bind(this);
    }


    @OnClick(R.id.tv1)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv1:

                AndPermission.with(this)
                        .runtime()
                        .permission(Permission.WRITE_EXTERNAL_STORAGE)
                        .rationale(new Rationale<List<String>>() {
                            @Override
                            public void showRationale(Context context, List<String> data, RequestExecutor executor) {
                                Log.e(TAG, "onClick: tv1==showRationale==data " + data+"      ");
                                Toast.makeText(PermissionActivity.this,"如果缺少权限将，无法使用！",Toast.LENGTH_LONG).show();
                                executor.execute();
                            }
                        })
                        .onGranted(new Action<List<String>>() {
                            @Override
                            public void onAction(List<String> data) {
                                Log.e(TAG, "onClick: tv1==onGranted==data " + data);
                            }
                        })
                        .onDenied(new Action<List<String>>() {
                            @Override
                            public void onAction(List<String> data) {
                                Log.e(TAG, "onClick: tv1==onDenied==data " + data);
                            }
                        }).start();

                break;
        }
    }
}
